﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Optimization;

namespace FaWorksSolution.MvcApplication
{
    public static class AssetsHelper
    {
        static AssetsHelper()
        {
            Load();
        }

        #region Base
        private static void Load() {
            DefaultContext asDb = new DefaultContext();
            __Groups = asDb.AssetsGroups.Include("Files.Cdns").ToList();
        }

        private static string GetVersionPrefix(string path)
        {
            return (path.IndexOf('?') > -1 ? "&" : "?") + "v=";
        }
        #endregion

        #region Javascript
        public static IHtmlString RenderJS(string attribute = "type=\"text/javascript\"", params string[] paths)
        {
            var sbScripts = new StringBuilder();

            foreach (var path in paths)
            {
                Models.Assets.Group group = __Groups.FirstOrDefault(m => m.GroupName.ToLower() == path.ToLower());

                if (group != null)
                {
                    foreach (var file in group.Files.OrderBy(m => m.Order))
                    {
                        var cdn = file.Cdns.FirstOrDefault(m => m.Useable == true);
                        if (cdn != null)
                        {
                            sbScripts.AppendFormat(jsFormmat, cdn.SitePath, "", attribute);
                        }
                        else
                        {
                            sbScripts.AppendFormat(jsFormmat, file.FilePath, GetVersionPrefix(path) + 
                                file.Version, attribute);
                        }
                    }
                }
                else
                {
                    if (path.StartsWith("//"))
                    {
                        sbScripts.AppendFormat(jsFormmat, path, "", attribute);
                    }
                    else
                    {
                        sbScripts.AppendFormat(jsFormmat, path, GetVersionPrefix(path) +
                            System.IO.File.GetLastWriteTime(HttpContext.Current.Server.MapPath("~") + path)
                            .ToString("yyyyMMddHHmmss"), attribute);
                    }
                }
            }

            return new HtmlString(sbScripts.ToString());
        }
        #endregion

        #region Css
        public static IHtmlString RenderCss(string attribute = "type=\"text/css\" rel=\"stylesheet\"", params string[] paths)
        {
            var sbLinks = new StringBuilder();

            foreach (var path in paths)
            {
                Models.Assets.Group group = __Groups.FirstOrDefault(m => m.GroupName.ToLower() == path.ToLower());

                if (group != null)
                {
                    foreach (var file in group.Files.OrderBy(m => m.Order))
                    {
                        var cdn = file.Cdns.FirstOrDefault(m => m.Useable == true);
                        if (cdn != null)
                        {
                            sbLinks.AppendFormat(cssFormmat, cdn.SitePath, "", attribute);
                        }
                        else
                        {
                            sbLinks.AppendFormat(cssFormmat, file.FilePath, GetVersionPrefix(path) + 
                                file.Version, attribute);
                        }
                    }
                }
                else
                {
                    if (path.StartsWith("//"))
                    {
                        sbLinks.AppendFormat(cssFormmat, path, "", attribute);
                    }
                    else
                    {
                        sbLinks.AppendFormat(cssFormmat, path, GetVersionPrefix(path) +
                            System.IO.File.GetLastWriteTime(HttpContext.Current.Server.MapPath("~") + path)
                            .ToString("yyyyMMddHHmmss"), attribute);
                    }
                }
            }

            return new HtmlString(sbLinks.ToString());
        }

        public static IHtmlString RenderLess(string attribute = "type=\"text/less\" rel=\"stylesheet\"", params string[] paths)
        {
            return RenderCss(attribute, paths);
        }
        #endregion

        private static IList<Models.Assets.Group> __Groups;
        const string jsFormmat = "<script src=\"{0}{1}\" {2}></script>";
        const string cssFormmat = "<link href=\"{0}{1}\" {2}>";
    }
}